package com.ruoyi.loss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.loss.domain.WellBase;
import com.ruoyi.loss.domain.bo.WellBaseBo;
import com.ruoyi.loss.domain.vo.WellBaseVo;
import com.ruoyi.loss.mapper.WellBaseMapper;
import com.ruoyi.loss.service.IWellBaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 钻井信息Service业务层处理
 *
 * @author yangyong
 * @date 2023-12-25
 */
@RequiredArgsConstructor
@Service
public class WellBaseServiceImpl implements IWellBaseService {

    private final WellBaseMapper baseMapper;

    /**
     * 查询钻井信息
     */
    @Override
    public WellBaseVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询钻井信息列表
     */
    @Override
    public TableDataInfo<WellBaseVo> queryPageList(WellBaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WellBase> lqw = buildQueryWrapper(bo);
        Page<WellBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钻井信息列表
     */
    @Override
    public List<WellBaseVo> queryList(WellBaseBo bo) {
        LambdaQueryWrapper<WellBase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WellBase> buildQueryWrapper(WellBaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WellBase> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWellNo()), WellBase::getWellNo, bo.getWellNo());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WellBase::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateDept()), WellBase::getCreateDept, bo.getCreateDept());
        return lqw;
    }

    /**
     * 新增钻井信息
     */
    @Override
    public Boolean insertByBo(WellBaseBo bo) {
        WellBase add = BeanUtil.toBean(bo, WellBase.class);
        add.setCreateDept(LoginHelper.getDeptId() + "");
        Assert.isTrue(checkWellNoUnique(add), "井号已存在");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改钻井信息
     */
    @Override
    public Boolean updateByBo(WellBaseBo bo) {
        WellBase update = BeanUtil.toBean(bo, WellBase.class);
        checkWellNoUnique(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private boolean checkWellNoUnique(WellBase well) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<WellBase>()
            .eq(WellBase::getWellNo, well.getWellNo())
            .ne(ObjectUtil.isNotNull(well.getId()), WellBase::getId, well.getId()));
        return !exist;
    }

    /**
     * 批量删除钻井信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
